package com.zdp.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdp.commons.GraceJSONResult;
import com.zdp.domain.User;
import com.zdp.mapper.UsersMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sesshomaru
 * @date 2021/7/2
 */
@RestController
@RequestMapping("/api")
public class UserResource {

    @Resource
    private UsersMapper usersMapper;



    @GetMapping("/user/{username}")
    public String getCurrentUsername(@PathVariable String username) {
        return "hello " + username;
    }

    @PreAuthorize("hashRole('ADMIN')")
    @GetMapping("/user/by-email/{email}")
    public User getUserByEmail(@PathVariable String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        User user = usersMapper.selectOne(queryWrapper);
        return user;
    }

    @GetMapping("/user/manager")
    public String getManager() {
        return "hello ";
    }








    @GetMapping("users")
    public GraceJSONResult users() {
        List<User> users = usersMapper.selectList(new QueryWrapper<>());
        return GraceJSONResult.ok(users);
    }

    @GetMapping("/greeting")
    public String greeting() {
        return "Hello World";
    }

    @PostMapping("/greeting")
    public String makeGreeting(@RequestParam String name,
                               @RequestBody Profile profile) {
        return "Hello World " + name + "!\n" + profile.gender;
    }

    @PostMapping("/greeting/{name}")
    public String putGreeting(@PathVariable String name) {
        return "Hello World " + name;
    }

    @GetMapping("/principal")
    public Authentication getPrincipal(Authentication authentication) {
        return authentication;
    }

    static class Profile{
        public String gender;
        public String idNo;
    }

}
